#include"Include.h"

volatile IH_Status_FLAG IH_Status_App;
volatile ControlSet_Flag	ControlSet_App;
volatile PanDetSet_Flag		PanDetSet_App;
volatile ProtectSet_Flag	ProtectSet_App;
volatile OtherSet_Flag		OtherSet_App;

bit bIIC_ERROR;			// I2c communication error bit
bit bInitSet;		// I2C init setting
vu8 PowerStep;
vu8 dataID,data,cmd;
vu8 PowerStep_buf,TargetTemp_buf,I2cError_Cnt;
vu8 pulseNum_buf,pulseWidth_buf;
vu8 Igbt_ovp_buf;
vu8 panOut_Current_buf;
bit bInitSet_pulseNum,bInitSet_pulseWidth;
bit bInitSet_PanOut_Cur;

static u8 iic_Rx_Step=0;
static u8 iic_Tx_Step=0;

//--------------------
vu8 Vol_Cal_H,Vol_Cal_L,Cur_Cal_H,Cur_Cal_L;
vu8 Undefine;

//---------------------	
void IIC_Init()
{
	SCL_C = 1;	//Input
	SDA_C = 1;
	
	_ifs00 = 0;	_ifs01 = 0;//SCL input source pin selection PA2
	_ifs02 = 0; _ifs03 = 0;	//SDA input source pin selection PA0	
	
	_iica = C_DEVICE_ID;			//Slave	address 0x70	
	_iicc0= 0b00000110;  		
	_iictoc=0b10011111; 		
	_iicf=0;   	
	_iice=1;
}

void IIC_TimeOut()  //
{	  
	if(I2cError_Cnt<= IIC_ERROR_NUM )	  
	{
		I2cError_Cnt++;
		if(1==bIIC_ERROR)  //iic error removal
		{
			bIIC_ERROR=0;
			bErrPowerOff=0;
		}
	}
	else
	{
		// Communication failure	
		bIIC_ERROR=1;  
		IH_Status_App.flagBit.error=ERROR_I2C;
		bErrPowerOff=1;		
		IGBT_Off();  
		u16ErrorBuf |= 0b00000100000000000;
	}	
}

volatile  u8 Check_Sum ;
void __attribute (( interrupt(0x3C) ))  I2c_ISR(void)//I2C Interrupt service routine
{
	volatile static u8 IIC_read_data[5];
	 	
	if(!_iictof)  // timeout
	{
		if(_haas)   //_haas=1,address match trig interrupt	
		{			//HASS = 1,			
			if(_srw)  //srw=1:slave in transfer mode
			{			
				_htx=1;	//htx=1,Slave to send mode   	
				_iicd=cmd;	//					
			}
			else  //srw=0:slave in receive mode		
			{	  
				_htx=0;		//htx = 0,
				_txak=0;	//txak = 0,
				_acc=_iicd;  //		
			}
		}
		else	//_haas=0,data trig interrupt	
		{	
			if(_htx)  	///htx=1:slave in write state;		
			{		  	
				if(_rxak)  //rxak=1:master stop receiving next byte,master releases scl bus
				{			
					_htx=0; _txak=0;	//htx = 0,slave in receive mode	;txak = 0,Slave to send mode  
					_acc=_iicd;  		//
				}	
				else //rxak=0:master wants to receive next byte;	
				{
					//Receive clear
					iic_Rx_Step=0;

					if(C_OPERATION_CMD == cmd) //
					{
						switch(iic_Tx_Step++)
						{
							case 0:	
									_iicd=Temp_IGBT;	
									Check_Sum=Temp_IGBT+cmd; 			                          	 
									break ;	
									//----------------------------	
							case 1:		
									_iicd=Temp_PAN;		//PAN TEMP 
									Check_Sum=Check_Sum+Temp_PAN;                       	
									break ;	
									//----------------------------
							case 2:	
									_iicd= C_NC;		//Coil Temp 
									Check_Sum=Check_Sum+C_NC;
									break ;	
									//----------------------------																
							case 3:											
									_iicd = IH_Status_App.m_data;				//
									Check_Sum=Check_Sum+IH_Status_App.m_data;		
									break ;
									//----------------------------		
							case 4:	
									_iicd = C_NC;   			//0
									Check_Sum=Check_Sum+C_NC;
									break ;
									//----------------------------		
							case 5:
									_iicd=Check_Sum;	//Check_Sum   							
									iic_Tx_Step=0;
									Check_Sum = 0;				
									break ;
									//----------------------------
							default :
									iic_Tx_Step=0;	
									Check_Sum = 0;
									break;
									//----------------------------
						}
					}
					//----------------------------
				}				
			}
			else   //htx=0:slave in read state	
			{
				iic_Tx_Step = 0;
				{
					switch(iic_Rx_Step++)
					{			
						case 0: 
								//iic_Rx_Step++;
								IIC_read_data[0]=_iicd;	//Receive the 1th byte	 
								_txak=0;	//txak = 0
								break;
								//----------------------------		
						case 1://Receive the 2nd byte	 
								IIC_read_data[1]=_iicd;//	
								_txak=0;				
								break;
								//----------------------------
						case 2://Receive 3rd byte    
								IIC_read_data[2]=_iicd;//		
								_txak=0;
								break;						
								//----------------------------
						case 3://Receive the 4th byte   
								IIC_read_data[3]=_iicd;		
								_txak=0;
								break;						
								//----------------------------
						case 4://Receive the 5th byte  (Checksum)
								IIC_read_data[4]=_iicd;			
								_txak=1;			// Slave stop receiving
								Check_Sum=IIC_read_data[0]+IIC_read_data[1]+IIC_read_data[2]+IIC_read_data[3];
								if(IIC_read_data[4]==Check_Sum)//check right
								{
									cmd = IIC_read_data[0];	//
									switch(cmd)
									{
										case C_OPERATION_CMD: 				//
												ControlSet_App.m_data=IIC_read_data[1];
												
												if(ControlSet_App.flagBit.p_t_set)
												{
													TargetTemp_buf = IIC_read_data[2];	//temp
												}
												else
												{
													PowerStep_buf=IIC_read_data[2];     //power
												}
												
												Undefine=IIC_read_data[3];	
												break;
												//------------------------
									}
									I2cError_Cnt=0;
									Check_Sum = 0;	
								}
								//------------------------
								else
								{
								//	_txak=1;    // NO ACK
								}
								Check_Sum = 0;
								iic_Rx_Step=0;
								break ;
								//------------------------
						default :
								iic_Rx_Step=0;
								break ;
								//------------------------							
					}
				}	
			}
		}	
	}	
	else  //Timeout occurred
	{
		_iictof=0;
		_iictoen=1;
	}
	_iicf=0;  //Clear the interrupt flag bit
}

